package ru.housewife.model;

import org.hibernate.annotations.Cascade;
import org.springframework.validation.Errors;

import javax.persistence.*;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static org.hibernate.annotations.CascadeType.ALL;
import static org.hibernate.annotations.CascadeType.DELETE_ORPHAN;

@Entity
@Table(name = "USERS_TABLES")
@SequenceGenerator(name = "users_tables", sequenceName = "USERS_TABLES_SEQ")
public class UsersTable {

    public static enum Type {
        WEEK_7(7),
        MONTH_31(31),
        N_DAYS_PERIOD(0);

        private int periodLength;

        Type(int periodLength) {
            this.periodLength = periodLength;
        }

        public int getPeriodLength() {
            return periodLength;
        }
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "users_tables")
    @Column(name = "ID")
    private Long id;

    @Column(name = "NAME", length = 30)
    private String name;

    @Enumerated(EnumType.STRING)
    @Column(name = "TYPE", length = 20, nullable = false)
    private Type type = Type.WEEK_7;

    @Column(name = "PERMANENT")
    private boolean permanent;

    @Column(name = "DATE_FROM")
    private Date from;

    @Column(name = "DATE_TO")
    private Date to;

    @Column(name = "PERIOD_LENGTH")
    private int periodLength;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "usersTable", cascade = CascadeType.ALL)
    @Cascade({ ALL, DELETE_ORPHAN })
    private List<DayMenu> dayMenus;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "USER_ID")
    private User user;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public boolean isPermanent() {
        return permanent;
    }

    public void setPermanent(boolean permanent) {
        this.permanent = permanent;
    }

    public Date getFrom() {
        return from;
    }

    public void setFrom(Date from) {
        this.from = from;
    }

    public Date getTo() {
        return to;
    }

    public void setTo(Date to) {
        this.to = to;
    }

    public int getPeriodLength() {
        return periodLength;
    }

    public void setPeriodLength(int periodLength) {
        this.periodLength = periodLength;
    }

    public List<DayMenu> getDayMenus() {
        return dayMenus == null ? Collections.<DayMenu>emptyList() : dayMenus;
    }

    public void setDayMenus(List<DayMenu> dayMenus) {
        this.dayMenus = dayMenus;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
